package com.learn.micro_mall.service.api.category.impl;

import com.learn.micro_mall.common.response.PlainResult;
import com.learn.micro_mall.common.util.PlainResultUtils;
import com.learn.micro_mall.domain.entity.category.CategoryEntity;
import com.learn.micro_mall.domain.service.category.InvalidCategoryRelationDomainService;
import com.learn.micro_mall.domain.service.category.ValidatorCategoryDomainService;
import com.learn.micro_mall.domain.vo.category.CategoryId;
import com.learn.micro_mall.repository.category.CategoryRepository;
import com.learn.micro_mall.service.api.category.CategoryService;
import com.learn.micro_mall.service.convertor.category.CategoryMapper;
import com.learn.micro_mall.service.request.category.CategoryCreateRequest;
import com.learn.micro_mall.service.request.category.CategoryDeleteRequest;
import com.learn.micro_mall.service.request.category.CategoryUpdateRequest;
import com.learn.micro_mall.service.response.category.CategoryCreateResponse;
import com.learn.micro_mall.service.response.category.CategoryDeleteResponse;
import com.learn.micro_mall.service.response.category.CategoryUpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/category")
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private ValidatorCategoryDomainService validatorCategoryDomainService;

    @Autowired
    private InvalidCategoryRelationDomainService invalidCategoryRelationDomainService;

    @Resource
    private CategoryRepository categoryRepository;

    @Override
    @RequestMapping("/create")
    public PlainResult<CategoryCreateResponse> createCategory(CategoryCreateRequest request) throws Exception {
        CategoryEntity categoryEntity = buildCategoryEntity(request);
        validatorCategoryDomainService.validatorParentCategoryExist(request.getParentCategoryId());
        validatorCategoryDomainService.validatorTheSameLevelCategoryNoExist(request);
        // 新增商品分类参数
        categoryRepository.insertCategory(categoryEntity);
        CategoryCreateResponse response = new CategoryCreateResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }

    private CategoryEntity buildCategoryEntity(CategoryCreateRequest request) {
        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setCategoryName(request.getCategoryName());
        String categoryId = request.getParentCategoryId() != null ? request.getParentCategoryId() + request.getCategoryName() : request.getCategoryName();
        categoryEntity.setCategoryId(CategoryId.builder().categoryId(categoryId).build());
        categoryEntity.setParentCategoryId(request.getParentCategoryId());
        categoryEntity.setCategoryParams(request.getCategoryParams());
        return categoryEntity;
    }

    @Override
    @RequestMapping("/update")
    public PlainResult<CategoryUpdateResponse> updateCategory(CategoryUpdateRequest request) throws Exception {
        CategoryEntity categoryEntity = CategoryMapper.INSTANCES.toCategoryEntity(request);
        validatorCategoryDomainService.validatorCategoryExist(request.getCategoryId());
        categoryRepository.updateCategory(categoryEntity);
        CategoryUpdateResponse response = new CategoryUpdateResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }

    @Override
    @RequestMapping("/delete")
    public PlainResult<CategoryDeleteResponse> deleteCategory(CategoryDeleteRequest request) throws Exception {
        CategoryId categoryId = CategoryId.builder().categoryId(request.getCategoryId()).build();
        //失效商品和分类的关系
        invalidCategoryRelationDomainService.invalidCategoryGoodsRelation(categoryId);
        categoryRepository.deleteCategory(categoryId);
        CategoryDeleteResponse response = new CategoryDeleteResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }
}
